package com.qian.service.ddj.serviceImpl;

import com.qian.api.ddj.model.Channel;
import com.qian.api.ddj.service.IChannelService;
import com.qian.service.ddj.dao.BusinessRepository;
import com.qian.service.ddj.dao.ChannelRepository;
import com.qian.service.ddj.entity.DdjBusiness;
import com.qian.service.ddj.entity.DdjChannel;
import com.yaowk.service.common.BaseRepository;
import com.yaowk.service.common.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

@Service
public class ChannelServiceImpl extends BaseServiceImpl<Channel, DdjChannel, Integer> implements IChannelService {

    @Autowired
    private ChannelRepository dao;
    @Autowired
    private BusinessRepository businessDao;

    protected BaseRepository<DdjChannel, Integer> getDao() {
        return dao;
    }

    protected DdjChannel model2Entity(Channel model) {
        DdjChannel ddjChannel = new DdjChannel();
        ddjChannel.setId(model.getId());
        ddjChannel.setName(model.getName());
        ddjChannel.setCode(model.getCode());
        return ddjChannel;
    }

    protected Channel entity2model(DdjChannel entity) {
        Channel channel = new Channel();
        channel.setId(entity.getId());
        channel.setName(entity.getName());
        channel.setCode(entity.getCode());
        return channel;
    }

    @Override
    public Channel save(Channel model) {
        if (model.isNew()) {
            model = simpleSave(model);
        } else {
            model = entity2model(dao.save(model2Entity(model)));
        }
        return model;
    }

    @Override
    public Channel findChannelByBusinessId(Integer id) {
        DdjBusiness ddjBusiness = businessDao.findOne(id);
        return entity2model(ddjBusiness.getBusinessGroup().getChannel());
    }
}
